/*================ user.c ======================*/ 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "user.h"


/* Create a new user struct */
user create_user(const char* name, const char* password, const char* locale){
	user person = (user) calloc(1, sizeof(struct _user));
	
	person->name = (char*) calloc(strlen(name), sizeof(char));
	person->password = (char*) calloc(strlen(password), sizeof(char));
	person->locale = (char*) calloc(strlen(locale), sizeof(char));
	
	strcpy(person->name, name);
	strcpy(person->password, password);
	strcpy(person->locale, locale);
	
	return person;
}


/* delete a user struct */
void delete_user(user *person){
	free((*person)->name);
	free((*person)->password);
	free((*person)->locale);
	*person = NULL; 
}

/* print to stdout a user struct */
void print_user(const user person){
	if(person != NULL){
		if (person->name != NULL || person->password != NULL || person->locale == NULL){
			printf("person name 	-%s\n", person->name);
			printf("person password -%s\n", person->password);
			printf("person locale 	-%s\n", person->locale);
		}
			else 
				printf("Erro: NULL person member\n");
	}
	else 
		printf("Erro: NULL person\n");
}


/* find a user by his name */
user find_user(user users[], int users_size , char* name){
	
	int i;
	for(i = 0; i < users_size; i++){
		if(strcmp(users[i]->name, name) == 0)
			return users[i];
	}
	return NULL;
}

int check_user(user users[], int users_size, char *name, char *pass, char *locale){
	int i;
	for(i = 0; users[i] != NULL && i < users_size; i++){
		if(strcmp(users[i]->name, name) == 0){
			if(strcmp(users[i]->password, pass) == 0){
				if(strcmp(users[i]->locale, locale) == 0){
					return 1;
				}
			}
		}
	}
	return 0;
}





